<template>
  <div class="searchTop">
    <div class="top">
      <div class="left" @click="$router.go(-1)">
        <svg class="icon" aria-hidden="true">
          <use xlink:href="#icon-fanhui"></use>
        </svg>
      </div>
      <div class="right">
        <van-search
          shape="round"
          v-model="state.searchValue"
          placeholder="请输入搜索关键词"
          @search="onSearch"
        />
      </div>
      <div class="center" @click="cancel">
        <svg class="icon" aria-hidden="true">
          <use xlink:href="#icon-shanchu"></use>
        </svg>
      </div>
    </div>
    <div class="history" v-show="state.searchSongs == 0">
      <div class="left">历史</div>
      <div class="center">
        <div
          class="keywordItem"
          v-for="(item, index) in state.keywordList"
          :key="index"
          @click="historySearch(item)"
        >
          <van-tag
            round
            type="primary"
            color="#ccc"
            text-color="#666"
            size="large"
            >{{ item }}</van-tag
          >
        </div>
      </div>
      <div class="right">
        <svg class="icon" aria-hidden="true">
          <use xlink:href="#icon-shanchu"></use>
        </svg>
      </div>
    </div>
    <!-- 播放列表 -->
    <div class="playList" v-show="state.searchSongs.length != 0">
      <div class="playListTop">
        <div class="playListTop_left">
          <svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-bofang"></use>
          </svg>
          <div class="title">播放全部</div>
          <div class="count">
            {{ `(共${state.searchSongs.length}首)` }}
          </div>
        </div>
        <div class="playListTop_right"></div>
      </div>
      <!-- 歌单list -->
      <div class="musicList">
        <div
          class="musicItem"
          v-for="(item, index) in state.searchSongs"
          :key="index"
        >
          <div class="left" @click="playMusic(index, item)">
            <div class="index">{{ index + 1 }}</div>
            <div class="content">
              <div class="title">{{ item.name }}</div>
              <div class="author">{{ item.album.name }}</div>
            </div>
          </div>
          <div class="right">
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-bofang1"></use>
            </svg>
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-31liebiao"></use>
            </svg>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import SearchTop from '@/components/SearchTop'
import { onBeforeMount, reactive, getCurrentInstance } from 'vue'
export default {
  name: 'SearchTop',
  setup() {
    const router = useRoute()
    const store = useStore()
    const { proxy } = getCurrentInstance()
    const state = reactive({
      searchValue: '',
      keywordList: [], //搜索关键字list
      searchSongs: [], // 搜索歌列表
    })
    // 渲染之前从localStorage取出keywordList
    onBeforeMount(() => {
      state.keywordList = localStorage.keywordList
        ? JSON.parse(localStorage.keywordList)
        : []
    })
    // search事件
    let onSearch = () => {
      state.keywordList.push(state.searchValue)
      //   去重
      state.keywordList = Array.from(new Set(state.keywordList))
      //   限制10个
      if (state.keywordList && state.keywordList.length > 10) {
        state.keywordList = state.keywordList.slice(
          state.keywordList.length - 10,
          state.keywordList.length
        )
      }
      // 存入localStorage
      localStorage.keywordList = JSON.stringify(state.keywordList)
      //   搜索音乐
      proxy.$axios
        .post(`http://localhost:3000/search?keywords=${state.searchValue}`)
        .then((res) => {
          console.log('搜索音乐', res.data.result)
          const { songs } = res.data.result
          console.log('songs', songs)
          state.searchSongs = [...songs]
          // store.commit('setSearchList', state.searchSongs)
        })
    }
    // 历史搜索-search
    let historySearch = (item) => {
      console.log(item)
      state.searchValue = item
      onSearch()
    }
    let playMusic = (index, item) => {
      // 处理数据
      item.al = item.album
      item.al.picUrl = item.album.artist.img1v1Url
      console.log(5555, item)
      console.log(6666, store.state.playList)
      store.commit('pushPlayList', item)
      console.log(777, store.state.playList)
      store.commit('setCurrentIndex', store.state.playList.length - 1)
    }
    let cancel = () => {
      state.searchValue = '';
      state.searchSongs = [];
    }
    return {
      router,
      state,
      onSearch,
      historySearch,
      playMusic,
      cancel,
    }
  },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
.searchTop {
  .top {
    width: 100%;
    height: 1.2rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 0.2rem;
    box-sizing: border-box;
    border-bottom: 1px solid #ccc;
    .left {
      flex: 0.5;
      .icon {
        width: 0.5rem;
        height: 0.5rem;
        fill: #000;
      }
    }
    .right {
      flex: 9.5;
    }
    .center {
      flex:0.5;
            .icon {
        width: 0.5rem;
        height: 0.5rem;
        fill: #000;
      }
    }
  }
  .history {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: 0.2rem 0.2rem;
    box-sizing: border-box;
    .left {
      font-size: 0.4rem;
      font-weight: 700;
      margin-right: 0.2rem;
      flex: 1.5;
    }
    .center {
      flex: 8;
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      .keywordItem {
        margin: 0.1rem 0.1rem;
        color: #666;
      }
    }
    .right {
      flex: 0.5;
      .icon {
        width: 0.4rem;
        height: 0.4rem;
        fill: #000;
      }
    }
  }
  // playList
  .playList {
    margin-top: 0.5rem;
    background-color: #fff;
    border-radius: 0.4rem 0.4rem 0 0;
    .playListTop {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 0.8rem;
      padding: 0.3rem;
      line-height: 0.8rem;
      .playListTop_left {
        display: flex;
        .icon {
          width: 0.5rem;
          height: 0.5rem;
          fill: #000;
          margin-top: 0.1rem;
          margin-right: 0.2rem;
        }
        .title {
          color: #000;
          font-size: 0.35rem;
          margin-right: 0.2rem;
        }
        .count {
          color: #ccc;
          font-size: 0.2rem;
        }
      }
      .playListTop_right {
        // flex: 1;
        .collect {
          width: 2.5rem;
          height: 1rem;
          background-color: rgb(254, 57, 60);
          border-radius: 0.5rem;
          line-height: 1rem;
          color: #fff;
          text-align: center;
          font-size: 0.25rem;
        }
      }
    }
  }
  // musicList
  .musicList {
    .musicItem {
      display: flex;
      align-items: center;
      padding: 0 0.3rem;
      .left {
        flex: 8;
        position: relative;
        .index {
          width: 0.5rem;
          color: #ccc;
          position: absolute;
          top: 50%;
          left: 0;
          transform: translate(0, -50%);
        }
        .content {
          margin-left: 0.2rem;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          padding: 0.3rem;
          .title {
            font-size: 0.3rem;
            color: #000;
            font-weight: 600;
            padding-bottom: 0.2rem;
          }
          .author {
            color: #ccc;
            font-size: 0.2rem;
            height: 0.4rem;
            line-height: 0.4rem;
          }
          .tag {
            font-size: 0.1rem;
            color: orangered;
            border: 1px solid orangered;
            border-radius: 0.05rem;
            &:last-child {
              margin-right: 0.1rem;
            }
          }
        }
      }
      .right {
        flex: 2;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .icon {
          width: 0.5rem;
          height: 0.5rem;
          fill: #ccc;
        }
      }
    }
  }
}
</style>
